<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.search" placeholder="请输入查询内容" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-button class="filter-item" type="primary" icon="el-icon-search" style="margin-left: 10px;" @click="handleFilter">
        搜索
      </el-button>
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="handleCreate">
        创建代理
      </el-button>
    </div>
    <el-table
      :data="list"
      border
      style="width: 100%;margin-top: 10px;"
    >
      <el-table-column
        prop="id"
        label="序号"
        min-width="20%"
      />
      <el-table-column
        prop="username"
        label="登陆账号"
        min-width="30%"
      />
      <el-table-column
        prop="nickname"
        label="姓名"
        min-width="30%"
      />
      <el-table-column
        prop="mobile"
        label="手机号"
        min-width="20%"
      />
      <el-table-column
        prop="agent_level"
        label="代理等级"
        min-width="20%"
      />
      <el-table-column
        prop="port"
        label="可使用端口"
        min-width="20%"
      />
      <el-table-column label="操作" min-width="20%">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleRecharge(scope.$index, scope.row)"
          >充值</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleReduce(scope.$index, scope.row)"
          >扣除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      background
      layout="prev, pager, next"
      :total="total"
      @current-change="handleCurrentChange"
    />

    <el-dialog title="创建代理账户" :visible.sync="createAgentFormVisible">
      <el-form ref="createAgentForm" :rules="createAgentFormRules" :model="createAgentForm" label-position="left" label-width="120px" style="width: 400px; margin-left:50px;">
        <el-form-item label="登陆账户" prop="username">
          <el-input v-model="createAgentForm.username" />
        </el-form-item>
        <el-form-item label="公司名" prop="nickname">
          <el-input v-model="createAgentForm.nickname" />
        </el-form-item>
        <el-form-item label="登陆密码" prop="password">
          <el-input v-model="createAgentForm.password" type="password" />
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
          <el-input v-model="createAgentForm.mobile" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="createAgentFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="createAgent()">
          确认
        </el-button>
      </div>
    </el-dialog>

    <el-dialog title="充值端口" :visible.sync="rechargePortFormVisible">
      <el-form ref="rechargePortForm" :rules="rechargePortFormRules" :model="rechargePortForm" label-position="left" label-width="120px" style="width: 400px; margin-left:50px;">
        <el-form-item label="登陆账户" prop="username">
          <el-input v-model="rechargePortForm.username" :disabled="true" />
        </el-form-item>
        <el-form-item label="公司名" prop="nickname">
          <el-input v-model="rechargePortForm.nickname" :disabled="true" />
        </el-form-item>
        <el-form-item label="增加端口数" prop="port">
          <el-input v-model="rechargePortForm.port" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="rechargePortFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="rechargePort()">
          确认
        </el-button>
      </div>
    </el-dialog>

    <el-dialog title="扣除端口" :visible.sync="reducePortFormVisible">
      <el-form ref="reducePortForm" :rules="reducePortForm" :model="reducePortForm" label-position="left" label-width="120px" style="width: 400px; margin-left:50px;">
        <el-form-item label="登陆账户" prop="username">
          <el-input v-model="reducePortForm.username" :disabled="true" />
        </el-form-item>
        <el-form-item label="公司名" prop="nickname">
          <el-input v-model="reducePortForm.nickname" :disabled="true" />
        </el-form-item>
        <el-form-item label="扣除端口数" prop="port">
          <el-input v-model="reducePortForm.port" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="reducePortFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="reducePort()">
          确认
        </el-button>
      </div>
    </el-dialog>
  </div>

</template>

<script>
import { getAgentList, createAgent, rechargePort, reducePort } from '../../api/agent.js'
import { Message } from 'element-ui'
export default {
  data() {
    return {
      list: null,
      total: 0,
      createAgentForm: {
        username: '',
        nickname: '',
        password: '',
        mobile: ''
      },
      createAgentFormVisible: false,
      createAgentFormRules: {
        username: [{ required: true, message: '用户名必须填写', trigger: 'blur' }],
        nickname: [{ required: true, message: '公司名必须填写', trigger: 'blur' }],
        password: [{ required: true, message: '密码必须填写', trigger: 'blur' }],
        mobile: [{ required: true, message: '手机号必须填写', trigger: 'blur' }]
      },
      rechargePortForm: {
        id: 0,
        nickname: '',
        username: '',
        port: 0
      },
      rechargePortFormVisible: false,
      rechargePortFormRules: {
        port: [{ required: true, message: '端口必须填写', trigger: 'blur' }]
      },
      reducePortForm: {
        id: 0,
        nickname: '',
        username: '',
        port: 0
      },
      reducePortFormVisible: false,
      reducePortFormRules: {
        port: [{ required: true, message: '端口必须填写', trigger: 'blur' }]
      },
      listQuery: {
        page: 1,
        search: ''
      }
    }
  },
  created() {
    this.getAgentList()
  },
  methods: {
    getAgentList() {
      this.listLoading = true
      getAgentList(this.listQuery).then(response => {
        this.list = response.data.data
        this.total = response.data.total
      })
    },
    handleRecharge(index, row) {
      console.log(index, row)
      this.rechargePortForm.id = row.id
      this.rechargePortForm.nickname = row.nickname
      this.rechargePortForm.username = row.username
      this.rechargePortFormVisible = true
    },
    handleReduce(index, row) {
      console.log(index, row)
      this.reducePortForm.id = row.id
      this.reducePortForm.nickname = row.nickname
      this.reducePortForm.username = row.username
      this.reducePortFormVisible = true
    },

    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getAgentList()
    },
    resetRow() {
      this.row = {
        username: '',
        nickname: '',
        password: '',
        mobile: ''
      }
    },
    handleCreate() {
      this.resetRow()
      this.createAgentFormVisible = true
      this.$nextTick(() => {
        this.$refs['createAgentForm'].clearValidate()
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getAgentList()
    },
    createAgent() {
      this.$refs['createAgentForm'].validate((valid) => {
        if (valid) {
          createAgent(this.createAgentForm).then(response => {
            if (response.code === 1) {
              Message({
                message: response.msg,
                type: 'success',
                duration: 5 * 1000
              })
              this.createAgentFormVisible = false
              this.getAgentList()
            }
          })
        }
      })
    },
    rechargePort() {
      this.$refs['rechargePortForm'].validate((valid) => {
        if (valid) {
          rechargePort(this.rechargePortForm).then(response => {
            if (response.code === 1) {
              Message({
                message: response.msg,
                type: 'success',
                duration: 5 * 1000
              })
              this.rechargePortFormVisible = false
              this.getAgentList()
            }
          })
        }
      })
    },
    reducePort() {
      this.$refs['reducePortForm'].validate((valid) => {
        if (valid) {
          reducePort(this.reducePortForm).then(response => {
            if (response.code === 1) {
              Message({
                message: response.msg,
                type: 'success',
                duration: 5 * 1000
              })
              this.reducePortFormVisible = false
              this.getAgentList()
            }
          })
        }
      })
    }
  }
}
</script>
